package com.tengju.data.infrastructure.service;

import com.tengju.data.domain.model.report.AchievementBonusDateConfig;
import com.tengju.data.domain.model.report.DataAchievementBonusDateConfigType;
import com.tengju.data.domain.model.service.AchievementBonusDateConfigService;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.DataAchievementBonusDateConfigDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.tengju.custom.DataAchievementBonusDateConfigMapper;
import com.tengju.data.infrastructure.service.convert.AchievementBonusDateConfigConvert;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;

/**
 * @author chenhang
 * @Date 2021/9/18
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class AchievementBonusDateConfigServiceImpl implements AchievementBonusDateConfigService {

    private final DataAchievementBonusDateConfigMapper bonusDateConfigMapper;

    @Override
    public AchievementBonusDateConfig queryByCalDateOrDefault(LocalDate queryDate) {
        AchievementBonusDateConfig dateConfig = queryByCalDate(queryDate);
        if (dateConfig == null) {
            return new AchievementBonusDateConfig(queryDate);
        }
        return dateConfig;
    }

    @Override
    public AchievementBonusDateConfig queryByCalDate(LocalDate queryDate) {
        List<DataAchievementBonusDateConfigDO> configList =  bonusDateConfigMapper.listConfig(queryDate,DataAchievementBonusDateConfigType.DATA_RANGE.getCode());
        if (CollectionUtils.isEmpty(configList)) {
            return null;
        }
        return AchievementBonusDateConfigConvert.convertToModel(configList);
    }

    @Override
    public AchievementBonusDateConfig queryByDateKeyOrDefault(String dateKey,LocalDate curDate) {
        if (StringUtils.isBlank(dateKey)) {
            return new AchievementBonusDateConfig(curDate);
        }
        List<DataAchievementBonusDateConfigDO> configList = bonusDateConfigMapper.listByDateKey(dateKey);
        return AchievementBonusDateConfigConvert.convertToModel(configList);
    }

    @Override
    public AchievementBonusDateConfig queryByDateKey(String dateKey) {
        if (StringUtils.isBlank(dateKey)) {
            return null;
        }
        List<DataAchievementBonusDateConfigDO> configList = bonusDateConfigMapper.listByDateKey(dateKey);
        return AchievementBonusDateConfigConvert.convertToModel(configList);
    }

    @Override
    public AchievementBonusDateConfig queryByLastCalDate(LocalDate lastCalDate) {
        if (lastCalDate == null) {
            return null;
        }
        List<DataAchievementBonusDateConfigDO> configList =  bonusDateConfigMapper.listConfig(lastCalDate,DataAchievementBonusDateConfigType.LAST_CAL_DATE.getCode());
        return AchievementBonusDateConfigConvert.convertToModel(configList);
    }

    @Override
    public AchievementBonusDateConfig queryBySettleDate(LocalDate settleDate) {
        if (settleDate == null) {
            return null;
        }
        List<DataAchievementBonusDateConfigDO> configList =  bonusDateConfigMapper.listConfig(settleDate,DataAchievementBonusDateConfigType.SETTLE_DATE.getCode());
        return AchievementBonusDateConfigConvert.convertToModel(configList);
    }

    @Override
    public void save(AchievementBonusDateConfig dateConfig) {
        List<DataAchievementBonusDateConfigDO> records = AchievementBonusDateConfigConvert.convertToDOList(dateConfig);
        if (CollectionUtils.isEmpty(records)) {
            return;
        }
        bonusDateConfigMapper.batchInsert(records);
    }

    @Override
    public AchievementBonusDateConfig queryByRiskDate(LocalDate queryDate) {
        if (queryDate == null) {
            return null;
        }
        List<DataAchievementBonusDateConfigDO> configList =  bonusDateConfigMapper.listConfig(queryDate,DataAchievementBonusDateConfigType.CAL_RISK.getCode());
        return AchievementBonusDateConfigConvert.convertToModel(configList);
    }
}
